﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Core.Contract.DataContract;
using Data.BO;
using www.Models;

namespace Controllerss
{
    public class LoginController
    {
        #region value
        private readonly LoginBO _loginBo=new LoginBO();
        private readonly MemberBO _memberBo=new MemberBO();
        #endregion

        #region ham lay du lieu

        public IQueryable<RelaxLoginsDataContract> GetAllUser()
        {
            using (var scope = new TransactionScope())
            {
                var lReturn = _loginBo.GetAllUser();
                scope.Complete();
                return lReturn;
            }
        }

        public IQueryable<RelaxLoginsDataContract> GetRelaxLoginsDataContracts(string sUserName)
        {
            using (var scope = new TransactionScope())
            {
                var lReturn = _memberBo.GetMemberDataContracts(sUserName);
                scope.Complete();
                return lReturn;
            }
        }
        public IQueryable<RelaxLoginsDataContract> GetAllClientUser()
        {
            using (var scope = new TransactionScope())
            {
                var lReturn = _loginBo.GetAllClientUser();
                scope.Complete();
                return lReturn;
            }
        }

        public IQueryable<RelaxLoginsDataContract> GetRelaxLoginsDataContracts()
        {
            using (var scope = new TransactionScope())
            {
                var lReturn = _memberBo.GetAllMemberDataContracts();
                scope.Complete();
                return lReturn;
            }
        }
        #endregion

        #region Add

        public long Add(RelaxLoginsDataContract loginsDataContract)
        {
            using (var scope = new TransactionScope())
            {
                var iD = 0;
                var member = new RelaxMember
                {
                    name = loginsDataContract.MemberName,
                    birthday = loginsDataContract.Birthday,
                    gender = loginsDataContract.Gender,
                    hometown = loginsDataContract.Hometown,
                    degree = loginsDataContract.Degree,
                    mail = loginsDataContract.Email,
                    tel = loginsDataContract.Tel,
                    image = loginsDataContract.Image,
                    active = loginsDataContract.Active
                };

                 var login = new RelaxLogin
                {
                    username = loginsDataContract.UserName,
                    password = loginsDataContract.Password,
                    active = loginsDataContract.Active,
                    role = loginsDataContract.Role
                };

                _memberBo.Add(member);
                var firstMember = _memberBo.GetMbDataContracts().OrderByDescending(o => o.Id).FirstOrDefault();
                if (firstMember != null)
                    login.memberid = Convert.ToInt32(firstMember.Id);

                _loginBo.Add(login);

                var first = _loginBo.GetAllUser().OrderByDescending(o => o.Id).FirstOrDefault();
                if (first != null)
                    iD = Convert.ToInt32(first.Id);
                scope.Complete();
                return iD;
            }
        }

        public long AddLogin(RelaxLoginsDataContract loginsDataContract)
        {
            using (var scope = new TransactionScope())
            {
                long iD = 0;
                var login = new RelaxLogin
                {
                    username = loginsDataContract.UserName,
                    password = loginsDataContract.Password,
                    active = loginsDataContract.Active,
                    Staffid = Convert.ToInt32(loginsDataContract.StaffId),
                    role = loginsDataContract.Role
                };
               _loginBo.Add(login);
               var first = _loginBo.GetAllUser().OrderByDescending(o => o.Id).FirstOrDefault();
               if (first != null)
                   iD = Convert.ToInt32(first.Id);
               scope.Complete();
               return iD;
            }
        }
        #endregion

        #region Update

        public Boolean Update(RelaxLoginsDataContract loginsDataContract)
        {
            using (var scope = new TransactionScope())
            {
                var member = new RelaxMember
                {
                    id = Convert.ToInt32(loginsDataContract.MemberId),
                    name = loginsDataContract.MemberName,
                    birthday = loginsDataContract.Birthday,
                    gender = loginsDataContract.Gender,
                    hometown = loginsDataContract.Hometown,
                    degree = loginsDataContract.Degree,
                    mail = loginsDataContract.Email,
                    tel = loginsDataContract.Tel,
                    image = loginsDataContract.Image,
                    active = loginsDataContract.Active
                };

                var login = new RelaxLogin
                {
                    id = Convert.ToInt32(loginsDataContract.Id),
                    username = loginsDataContract.UserName,
                    password = loginsDataContract.Password,
                    active = loginsDataContract.Active,
                    role = loginsDataContract.Role
                };

                _memberBo.Update(member);
                _loginBo.Update(login);

                scope.Complete();
                return true;
            }
        }
        public Boolean UpdateLogin(RelaxLoginsDataContract loginsDataContract)
        {
            using (var scope = new TransactionScope())
            {
                var login = new RelaxLogin
                {
                    id=Convert.ToInt32(loginsDataContract.Id),
                    username = loginsDataContract.UserName,
                    password = loginsDataContract.Password,
                    active = loginsDataContract.Active,
                    role = loginsDataContract.Role
                };
                _loginBo.Update(login);
               
                scope.Complete();
                return true;
            }
        }
        #endregion

        #region Delete

        public Boolean Delete(List<long> lId)
        {
            using (var scope = new TransactionScope())
            {
                foreach (var id in lId)
                {
                    _memberBo.Delete(id);
                    var first = _loginBo.GetAllUser().FirstOrDefault(p => p.MemberId == id);
                    if (first != null)
                    {
                        _loginBo.Delete(Convert.ToInt32(first.Id));   
                    }
                }
                scope.Complete();
                return true;
            }
            
        }
        public Boolean DeleteLogin(long id)
        {
            using (var scope = new TransactionScope())
            {
                _loginBo.Delete(id);
                scope.Complete();
                return true;
            }

        }
        #endregion
    }
}
